<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-right">
        <div class="title-container">
          <span>{{ $t("common.system") }}</span>
        </div>
        <el-form ref="loginForm" :model="loginForm">
          <el-form-item prop="username" :rules="[{ required: true, trigger: 'blur', message: $t('home.account') }]">
            <div class="input-box">
              <span class="svg-container">
                <img class="svg-img" src="../../assets/icon/login-user.png" alt>
              </span>
              <el-input
                v-model="loginForm.username"
                :placeholder="$t('home.account')"
                name="username"
                type="text"
                auto-complete="on"
              />
            </div>
          </el-form-item>

          <el-form-item prop="password" :rules="[{ required: true, trigger: 'blur', message: $t('home.pwd') }]">
            <div class="input-box">
              <span class="svg-container">
                <img class="svg-img" src="../../assets/icon/login-pwd.png" alt>
              </span>
              <el-input
                :key="passwordType"
                v-model="loginForm.password"
                :type="passwordType"
                :placeholder="$t('home.pwd')"
                name="password"
                auto-complete="on"
                @keyup.enter.native="handleLogin"
              />
            </div>
          </el-form-item>
        </el-form>
        <div class="login-btn" @click="handleLogin">{{ $t('common.login') }}</div>
      </div>
    </div>
  </div>
</template>

<script>
// eslint-disable-next-line no-unused-vars
import { validUsername } from '@/utils/validate'
import { getLang, setLang } from '@/utils/auth'

export default {
  name: 'Login',
  data() {
    return {
      lang: getLang() || 'zh',
      loginForm: {
        username: '',
        password: ''
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          const password = this.$util.MD5(this.loginForm.password)
          const data = {
            username: this.loginForm.username,
            password: password
          }
          // return
          this.$store
            .dispatch('LoginByUsername', data)
            .then(() => {
              console.log(this.redirect)
              this.loading = false
              this.$router.push({ path: this.redirect || '/' })
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    switchLang(val) {
      this.$refs['loginForm'].clearValidate()
      this.$i18n.locale = val // 此处val为 zh 或者 en
      setLang(val)
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

// @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
//   .login-container .el-input input {

//   }
// }

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    vertical-align: middle;

    input {
      background: #ffffff;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 10px;
      color: #313131;
      height: 47px;
    }
    :-webkit-autofill,
    :-webkit-autofill:hover,
    :-webkit-autofill:focus,
    :-webkit-autofill:active {
      // 回填文字颜色设置
      -webkit-text-fill-color: #313131 !important;
      transition: background-color 5000s ease-in-out 0s;
    }
  }
  .el-form-item__error {
    color: #f56c6c;
    font-size: 12px;
    line-height: 1;
    padding: 0;
    top: auto;
    position: absolute;
    left: 50px !important;
    bottom: 2px !important;
  }
}
</style>

<style lang="scss" scoped>
.login-container {
  min-height: 100%;
  width: 100%;
  background-color: #f9f9f9;
  overflow: hidden;

  .login-box {
    width: 960px;
    height: 433px;
    position: absolute;
    left: 50%;
    top: 50%;
    margin: -235px 0px 0px -480px;
    background: #ffffff url("../../assets/404_images/login-bg.png") no-repeat
      left center;
    box-shadow: -2px 2px 5px rgba(0, 0, 0, 0.13),
      2px 2px 5px rgba(0, 0, 0, 0.13);
  }

  .login-right {
    width: 420px;
    height: 100%;
    float: right;
    background-color: #fff;
    position: relative;

    .title-container {
      width: 100%;
      height: 125px;
      line-height: 125px;
      font-size: 30px;
      color: #1f1f1f;
      background: url("../../assets/icon/small-logo.png") no-repeat 50px center;
      background-size: 30px;
      font-weight: bold;
      text-indent: 120px;
    }
    .el-form-item {
      margin-bottom: 0;
    }
    //input:-internal-autofill-selected {
    //  background-color: #ffffff !important;
    //}

    .input-box {
      border: 1px solid #dcdcdc;
      background: #ffffff;
      border-radius: 5px;
      color: #313131;
      margin: 0 0 20px 50px;
      width: 318px;
    }
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding-left: 10px;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    .svg-img {
      vertical-align: middle;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    cursor: pointer;
    user-select: none;
  }

  .login-btn {
    width: 320px;
    height: 50px;
    line-height: 50px;
    font-size: 22px;
    color: #fff;
    text-align: left;
    cursor: pointer;
    margin: 0px 0px 0px 50px;
    background: #1890ff url("../../assets/icon/arrow.png") no-repeat 195px
      center;
    text-indent: 125px;
    border-radius: 5px;
  }
  .lang-box{
    margin: 20px 0 0 50px;

  }
}
</style>
